﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.View;
using TelnetRouterApplication.Control;
using TelnetRouterApplication.Connection;

namespace TelnetRouterApplication
{
    public partial class MainForm : Form
    {
        public static bool DEBUG = false;

        public MainForm()
        {
            InitializeComponent();
        }

        private void conectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConnectForm connectForm = new ConnectForm(this);
            connectForm.ShowDialog();
        }

        public void blockCommands(bool blocked)
        {
            //Blocked = true é quando você desconecta, Blocked = false é quando conecta.
            if (blocked)
            {
                conectToolStripMenuItem.Text = "Desconnect";
                //TODO mudar o método
            }
            else
            {
                conectToolStripMenuItem.Text = "Connect";
                //TODO mudar o método
            }

            this.settingsToolStripMenuItem.Enabled = !blocked;
            this.routingToolStripMenuItem.Enabled = !blocked;
            this.sendMessageToolStripMenuItem.Enabled = !blocked;
            this.interfacesToolStripMenuItem.Enabled = !blocked;
            this.checkConnectionToolStripMenuItem.Enabled = !blocked;
            this.commandsListToolStripMenuItem.Enabled = !blocked;
            this.monitorToolStripMenuItem.Enabled = !blocked;
        }

        public void changeStatus(IpModel ipModel)
        {
            this.toolStripStatusLabel1.Text = "Ip Connected: "+ipModel.ip;
            this.toolStripStatusLabel2.Text = "Port Connected: "+ipModel.port;

            
            CallSettingsForm();

            if (ConfigutarionHandler.GetInstance().RouterSettings.IsRouter)
            {
                this.switchingToolStripMenuItem.Enabled = false;
            }
            else
            {
                this.switchingToolStripMenuItem.Enabled = true;
            }
             
        }

        private void debugOFFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DEBUG)
            {
                this.debugOFFToolStripMenuItem.Text = "Debug OFF";
                MessageBox.Show("Debug desligado.");
            }
            else
            {
                this.debugOFFToolStripMenuItem.Text = "Debug ON";
                MessageBox.Show("Debug ligado.");
            }
            DEBUG = !DEBUG;

            #region setando o valor do debug nas classes
            ConnectForm.DEBUG = DEBUG;
            ConnectionHandler.DEBUG = DEBUG;
            TelnetConnection.DEBUG = DEBUG;
            RouterLevelHandler.DEBUG = DEBUG;
            CommandHandler.DEBUG = DEBUG;
            #endregion
        }

        private void sendMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SendMessageForm sendMessageForm = new SendMessageForm();
            sendMessageForm.ShowDialog();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CallSettingsForm();
        }

        public void CallSettingsForm()
        {
            mainPanelClear();
            mainPanel.Controls.Add(new SettingsPanel());
        }

        private void checkConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CallTestInterfacesForm();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void staticToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainPanelClear();
            mainPanel.Controls.Add(new StaticRouteForm());
        }

        private void rIPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainPanelClear();
            mainPanel.Controls.Add(new RIPForm());
        }

        private void statusToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CallInterfaceStatusForm();
        }

        public void CallTestInterfacesForm()
        {
            mainPanelClear();
            mainPanel.Controls.Add(new CheckConnection());
        }

        public void CallInterfaceStatusForm()
        {
            mainPanelClear();
            mainPanel.Controls.Add(new InterfaceStatus(this));
        }

        private void wANToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainPanelClear();
            mainPanel.Controls.Add(new WanSelectForm(this));
        }

        private void commandsListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainPanelClear();
            mainPanel.Controls.Add(new CommandListPanel());
        }

        private void lANToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainPanelClear();
            mainPanel.Controls.Add(new LanSelectForm(this));
        }

        private void monitorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainPanelClear();
            mainPanel.Controls.Add(new MonitorForm(this));
        }

        private void mainPanelClear()
        {
            for (int i = 0; i < mainPanel.Controls.Count; i++)
            {
                mainPanel.Controls[i].Dispose();
            }

            mainPanel.Controls.Clear();
        }

        private void vLANToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainPanelClear();
            mainPanel.Controls.Add(new VLANForm(this));
        }
    }
}
